package com.jingdianjichi.subject.application.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.jingdianjichi.subject.application.convert.SubjectLabelBOConverter;
import com.jingdianjichi.subject.application.service.SubjectLabelApplicationService;
import com.jingdianjichi.subject.domain.entity.SubjectLabelBO;
import com.jingdianjichi.subject.infra.basic.entity.SubjectLabel;
import com.jingdianjichi.subject.infra.basic.entity.SubjectMapping;
import com.jingdianjichi.subject.infra.basic.service.SubjectLabelService;
import com.jingdianjichi.subject.infra.basic.service.SubjectMappingService;
import com.jingdinajichi.subject.common.enmus.IsDeleteCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zwy
 * @version 1.0
 * @description: SubjectLabelApplicationServiceImpl
 * @date 2025/10/4 14:48
 */
@Service
@Slf4j
public class SubjectLabelApplicationServiceImpl implements SubjectLabelApplicationService {
    @Resource
    private SubjectLabelService subjectLabelService;
    @Resource
    private SubjectMappingService subjectMappingService;
    @Override
    public int add(SubjectLabelBO subjectLabelBO) {
        if(log.isInfoEnabled()){
            log.info("SubjectLabelApplicationServiceImpl.add.bo:{}",subjectLabelBO);
        }
        SubjectLabel subjectLabel = SubjectLabelBOConverter.INSTANCE
                .convertBoToLabel(subjectLabelBO);
        subjectLabel.setIsDeleted(IsDeleteCodeEnum.NOT_DELETE_CODE_ENUM.getCode());
        return   subjectLabelService.insert(subjectLabel);
    }

    /**
     * 查询岗位大类
     */

    /**
     * 查询题目分类小类
     */
//    @Override
//    public List<SubjectLabelBO> queryLabel(SubjectLabelBO subjectLabelBO) {
//
//        try{
//            if(log.isInfoEnabled()){
//                log.info("SubjectLabelApplicationServiceImpl.queryLabelByParentId.BO:{}",subjectLabelBO);
//            }
//
//            SubjectLabel subjectLabel = SubjectLabelBOConverter.INSTANCE.convertBoToLabel(subjectLabelBO);
//            subjectLabel.setIsDeleted(IsDeleteCodeEnum.NOT_DELETE_CODE_ENUM.getCode());
//            List<SubjectLabel> subjectCategories = subjectLabelService.queryLabel(subjectLabel);
//            List<SubjectLabelBO> subjectLabelBOS = SubjectLabelBOConverter.INSTANCE.convertLabelListToLabelBOList(subjectCategories);
//            return subjectLabelBOS;
//        }catch (Exception e){
//            log.error("SubjectLabelApplicationServiceImpl.queryLabelByParentId.error",e);
//            throw e;
//        }
//
//    }
//    /**
//     * 更新分类
//     */
    /**
     * 更新标签
     *
     * @param subjectLabelBO
     */
    @Override
    public boolean update(SubjectLabelBO subjectLabelBO) {
        try{
            if(log.isInfoEnabled()){
                log.info("SubjectLabelApplicationServiceImpl.update.BO:{}",subjectLabelBO);
            }
            SubjectLabel subjectLabel = SubjectLabelBOConverter.INSTANCE.convertBoToLabel(subjectLabelBO);
            int num = subjectLabelService.update(subjectLabel);
            return num > 0;
        }catch (Exception e){
            log.error("SubjectLabelApplicationServiceImpl.update.error",e);
            return false;
        }
    }
    //    /**
//     * 删除分类
//     */
//    /**
//     * 删除分类
//     *
//     * @param subjectLabelBO
//     */
    @Override
    public boolean delete(SubjectLabelBO subjectLabelBO) {
        SubjectLabel label = SubjectLabelBOConverter.INSTANCE.convertBoToLabel(subjectLabelBO);
        SubjectLabel subjectLabel = new SubjectLabel();
        subjectLabel.setId(label.getId());
        subjectLabel.setIsDeleted(IsDeleteCodeEnum.DELETE_CODE_ENUM.getCode());
        return subjectLabelService.update(subjectLabel) > 0;
    }
    /**
     * zwy
     * 2025/7/12 16:39
     * 根据分类id查询标签
     */
//    @Override
//    public List<SubjectLabelBO> queryLabelByCategoryId(SubjectLabelBO subjectLabelBO) {
//        try{
//            if(log.isInfoEnabled()){
//                log.info("SubjectLabelApplicationServiceImpl.queryLabelByParentId.BO:{}",subjectLabelBO);
//            }
//            SubjectLabel subjectLabel = SubjectLabelBOConverter.INSTANCE.convertBoToLabel(subjectLabelBO);
//            subjectLabel.setIsDeleted(IsDeleteCodeEnum.NOT_DELETE_CODE_ENUM.getCode());
//            List<SubjectLabel> subjectLabels = subjectLabelService.queryLabel(subjectLabel);
//            List<SubjectLabelBO> subjectLabelBOS = SubjectLabelBOConverter.INSTANCE.convertLabelListToLabelBOList(subjectLabels);
//            return subjectLabelBOS;
//        }catch (Exception e){
//            log.error("SubjectLabelApplicationServiceImpl.queryLabelByParentId.error",e);
//            throw e;
//        }
//
//    }

    /**
     * 根据分类id查询标签有题目的才查询
     *jc
     * @param subjectLabelBO
     */
    @Override
    public List<SubjectLabelBO> queryLabelByCategoryId(SubjectLabelBO subjectLabelBO) {
        Long categoryId = subjectLabelBO.getCategoryId();
        SubjectMapping subjectMapping = new SubjectMapping();
        subjectMapping.setCategoryId(categoryId);
        subjectMapping.setIsDeleted(IsDeleteCodeEnum.NOT_DELETE_CODE_ENUM.getCode());
        try{
            if(log.isInfoEnabled()){
                log.info("SubjectLabelApplicationServiceImpl.queryLabelByCategoryId.BO:{}",subjectLabelBO);
            }
//            让有题目的标签展示出来
            List<SubjectMapping> subjectMappingList = subjectMappingService.queryLabelIdList(subjectMapping);
            if(CollectionUtils.isEmpty(subjectMappingList)){
                return Collections.emptyList();
            }
            List<Long> labelIdList = subjectMappingList.stream().map(SubjectMapping::getLabelId).collect(Collectors.toList());
            List<SubjectLabel> subjectLabels = subjectLabelService.batchQueryById(labelIdList);
            List<SubjectLabelBO> subjectLabelBOS = SubjectLabelBOConverter.INSTANCE.convertLabelListToLabelBOList(subjectLabels);
            return subjectLabelBOS;
        }
        catch (Exception e){
            log.error("SubjectLabelApplicationServiceImpl.queryLabelByCategoryId.error",e);
            throw e;
        }

    }
}
